<?php

function checkauth($thi, $method, $ezauth) {
    
    /////DEBUG: ALWAYS LOGGED IN
    $thi->$method();
    /////END DEBUG
    
    //do something before we load the page:
    //check to see if you are logged in
    /*
    $auth = $ezauth->authorize($method, true);
		if ($auth['authorize'] == 'yes') {
			$segments = array_slice($this->uri->segment_array(),2);	//redirect with method arguments by marlar on CodeIgniter forums
			call_user_func_array(array(&$this, $method), $segments);
		} else {
		    // user login information incorrect, so show login screen again
		    $this->login();
		}
    */
    
}

function me_sort($a, $b)
	{
		
		return strcmp($a->sortname, $b->sortname);
	} 

function construct_admin_controller($ezauth) {
    

		//this is the store program (ezauth purposes)
		//$ezauth->program = 'store';		
		
		//automatically login if user has cookie hash saved
		//$ezauth->auto_login();		
		
		//this is the list of pages you need to be logged in to view
		//$ezauth->protected_pages = array(
		//			'all'			=>    	'user'
		//			);

}

function get_page_ready($location, $offset = 0, $SettingsModel, $PagesModel, $ProductsModel, $CategoriesModel)
{
    
    $data['offset'] = $offset;
    
    $data['options'] = $SettingsModel->get_options();
    
    $data['numpages'] = $PagesModel->number_pages();
    
    $data['numitems'] = $ProductsModel->number_items();
    
    $data['numcats'] = $CategoriesModel->number_categories();
    
    $data['numevents'] = $CategoriesModel->number_events();
    
    
    if ($location == 'products') {
           $data['oldestyear'] = $ProductsModel->get_oldest_year();
           if ($data['numitems'] > 0) {
               $data['items'] = $ProductsModel->get_items($offset);
               $data['categories'] = $CategoriesModel->get_hierarchy();
               $data['noitems'] = "FALSE";
           } else {
               $data['noitems'] = 'TRUE';
               $data['message'] = "No items! <a href='" . base_url() . "index.php/admin/product/add'>Click here to list an item</a>";
           }
    }
    
    if ($location == 'search') {
        $data['oldestyear'] = $ProductsModel->get_oldest_year();
           if ($data['numitems'] > 0) {
               $data['categories'] = $CategoriesModel->get_hierarchy();
               $data['noitems'] = "FALSE";
           } else {
               $data['noitems'] = 'TRUE';
               $data['message'] = "No items! <a href='" . base_url() . "index.php/admin/product/add'>Click here to list an item</a>";
           }
    }
    
    if ($location == 'pages') {
        $data['oldestyear'] = $PagesModel->get_oldest_year();
        if ($data['numpages'] > 0) {
            $data['noitems'] = 'FALSE';
            $data['pages'] = $PagesModel->get_pages($offset);
        } else {
            $data['noitems'] = 'TRUE';
            $data['message'] = "No pages! <a href='" . base_url() . "index.php/admin/page/add'>Click here to create a page</a>";
        }
    }
    
    
    if ($location == 'addcategory') {
        if ($data['numcats'] > 0) {
            $data['noitems'] = 'FALSE';
            $data['categories'] = $CategoriesModel->get_hierarchy();
        } else {
            $data['noitems'] = 'TRUE';
            $data['message'] = "No categories! <a href='" . base_url() . "index.php/admin/category/add'>Click here to add a category</a>";
        }
    }
    
    if ($location == "addproduct" || $location == "categories") {
        if ($data['numcats'] > 0) {
            $data['noitems'] = 'FALSE';
            $data['categories'] = $CategoriesModel->get_hierarchy();
        } else {
            $data['noitems'] = 'TRUE';
            $data['message'] = "No categories! <a href='" . base_url() . "index.php/admin/category/add'>Click here to add a category</a>";
        }
    }
    
    return $data;
}

?>